package com.java110.community.smo.impl;


import com.alibaba.fastjson.JSONObject;
import com.java110.community.dao.IRepairServiceDao;
import com.java110.core.base.smo.BaseServiceSMO;
import com.java110.dto.repair.*;
import com.java110.intf.community.IRepairInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName FloorInnerServiceSMOImpl
 * @Description 活动内部服务实现类
 * @Author wuxw
 * @Date 2019/4/24 9:20
 * @Version 1.0
 * add by wuxw 2019/4/24
 **/
@RestController
public class RepairInnerServiceSMOImpl extends BaseServiceSMO implements IRepairInnerServiceSMO {

    @Autowired
    private IRepairServiceDao repairServiceDao;




    @Override
    public  ResponseEntity<String>  saveRepairReturnVisit( RequestEntity requestEntity) {
        Object body = requestEntity.getBody();
        RepairReturnVisitDto dto = BeanConvertUtil.covertBean(body, RepairReturnVisitDto.class);
        String repairId = dto.getRepairId();
        String webContext = dto.getContext();
        RepairDto repairDto = repairServiceDao.getOneById(Long.valueOf(repairId));
        if (repairDto == null) {
            throw new CmdException("该维修单不存在");
        }
        if (!repairDto.getState().equals("1500")) {
            throw new CmdException("订单状态异常");
        }
        repairDto.setState(RepairStateEnum.COMPLETED.getState());
        repairDto.setStateName(RepairStateEnum.COMPLETED.getStateName());
        JSONObject jsonObject = BeanConvertUtil.beanCovertJson(repairDto);
        jsonObject.put("appointmentTime", repairDto.getAppointmentTime());
        jsonObject.put("creatTime", repairDto.getCreatTime());
        jsonObject.put("notes", webContext);
        //修改订单状态
        repairServiceDao.updateOwnerRepair(jsonObject);

        //获取上一次流转单。修改信息
        RepairStaffsFlow repairStaffsFlow = repairServiceDao.lastTimeRepairFlow(Long.valueOf(repairId));
        repairStaffsFlow.setEndTime(DateUtil.getCurrentDate());
        repairStaffsFlow.setTimeConsuming(System.currentTimeMillis() - repairStaffsFlow.getCreatTime().getTime());
        repairServiceDao.updateRepairDispatchFlow(repairStaffsFlow);
        //新增流转单
        //添加当前流转单
        repairStaffsFlow.setId(null);
        repairStaffsFlow.setCreatTime(DateUtil.getCurrentDate());
        repairStaffsFlow.setState(Integer.parseInt(repairDto.getState()));
        repairStaffsFlow.setStateName(repairDto.getStateName());
        repairStaffsFlow.setContext(webContext);
        JSONObject beanedCovertJson = BeanConvertUtil.beanCovertJson(repairStaffsFlow);
        beanedCovertJson.put("appointmentTime", repairDto.getAppointmentTime());
        beanedCovertJson.put("endTime", DateUtil.getCurrentDate());
        beanedCovertJson.put("createUserName", repairDto.getStaffName());
        beanedCovertJson.put("currentUserId", repairDto.getStaffId());
        repairServiceDao.repairDispatchFlow(beanedCovertJson);
        //保存回访内容表
        repairServiceDao.saveRepairReturnVisit(dto);

        int count =0;
        ResultVo resultVo = new ResultVo(10, count, "成功");
        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        return responseEntity;
    }

    @Override
    public ResponseEntity<String> queryRepairReturnVisit(String communityId) {
        List<RepairDto> repairDtos = repairServiceDao.queryRepairReturnVisit(communityId);
        int count = repairDtos.size();
        ResultVo resultVo = new ResultVo(10, count, repairDtos);
        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        return responseEntity;
    }

    @Override
    public Long saveOwnerRepair(RepairDto repairDto) {
        return repairServiceDao.saveOwnerRepair(repairDto);
    }

    @Override
    public List<RepairDto> listOwnerRepairs(JSONObject reqJson) {
        return repairServiceDao.listOwnerRepairs(reqJson);
    }

    @Override
    public void updateOwnerRepair(JSONObject reqJson) {
        repairServiceDao.updateOwnerRepair(reqJson);
    }

    @Override
    public void deleteOwnerRepair(JSONObject reqJson) {
        repairServiceDao.deleteOwnerRepair(reqJson);
    }

    @Override
    public void repairDispatch(JSONObject reqJson) {
        repairServiceDao.repairDispatch(reqJson);
    }

    @Override
    public RepairDto getOneById(Long repairId) {
        return repairServiceDao.getOneById(repairId);
    }

    @Override
    public void repairDispatchFlow(JSONObject reqJson) {
        repairServiceDao.repairDispatchFlow(reqJson);
    }

    @Override
    public List<RepairStaffsFlow> listRepairStaffs(JSONObject reqJson) {
        return repairServiceDao.listRepairStaffs(reqJson);
    }

    @Override
    public RepairStaffsFlow lastTimeRepairFlow(Long repairId) {
        return repairServiceDao.lastTimeRepairFlow(repairId);
    }

    @Override
    public void updateRepairDispatchFlow(RepairStaffsFlow repairStaffsFlows) {
        repairServiceDao.updateRepairDispatchFlow(repairStaffsFlows);
    }

    @Override
    public List<RepairDto> listStaffRepairs(JSONObject reqJson) {
        return repairServiceDao.listStaffRepairs(reqJson);
    }

    @Override
    public void repairStop(JSONObject reqJson) {
        repairServiceDao.repairStop(reqJson);
    }

    @Override
    public void repairStart(JSONObject reqJson) {
        repairServiceDao.repairStart(reqJson);
    }

    @Override
    public void repairFinish(List<RepairPhotoDto> beforePhotoMap) {
        repairServiceDao.repairFinish(beforePhotoMap);
    }

    @Override
    public void choosedGoodsList(List<HashMap<Object, Object>> choosedList) {
        repairServiceDao.choosedGoodsList(choosedList);
    }
}
